package top.lanscarlos.ashcraft;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;

/**
 * Ashcraft
 * top.lanscarlos.ashcraft
 *
 * @author Lanscarlos
 * @since 2022-05-25 12:40
 */
@Configuration
public class AshCraftConfig {

    @Bean
    public SessionFactory sessionFactory() {
        System.out.println("initSessionFactory...");
        return new org.hibernate.cfg.Configuration().configure().buildSessionFactory();
    }

    @Bean
    public Gson gson() {
        return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    }

    /**
     * 处理映射器
     * 自动将 Controller 中返回的对象转换为 JSON 字符串格式
     * */
    @Bean
    public GsonHttpMessageConverter gsonHttpMessageConverter() {
        return new GsonHttpMessageConverter(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create());
    }

}
